package logic

import (
	"context"
	"strconv"
	"time"

	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"gorm.io/gorm"

	"2106A-zg6/baidu/srv/internal/svc"
	"2106A-zg6/baidu/srv/models"
	"2106A-zg6/baidu/srv/pb/baidu"

	"github.com/zeromicro/go-zero/core/logx"
)

type ScoreChangeVipLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewScoreChangeVipLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ScoreChangeVipLogic {
	return &ScoreChangeVipLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ScoreChangeVipLogic) ScoreChangeVip(in *baidu.ScoreChangeVipRequest) (*baidu.ScoreChangeVipResponse, error) {

	err := models.UpdateStatusVipCard(in.VipCardId)
	if err != nil {
		return nil, status.Errorf(codes.Canceled, err.Error())
	}
	vId, _ := strconv.Atoi(in.VipId)
	UId, _ := strconv.Atoi(in.UserId)

	err = models.CreateVip(&models.Vip{
		UserID: int64(UId),
		VipId:  int64(vId),
		Model: gorm.Model{
			CreatedAt: time.Now(),
			UpdatedAt: time.Now(),
		},
	})
	if err != nil {
		return nil, status.Errorf(codes.Canceled, err.Error())
	}

	return &baidu.ScoreChangeVipResponse{}, nil
}
